package com.neusoft.weekreport.component.bizService.impl;

import com.neusoft.weekreport.component.bizService.RoleRefUserBizService;
import com.neusoft.weekreport.component.entities.BgUserRefRole;
import com.neusoft.weekreport.component.entities.BgUserRefRoleExample;
import com.neusoft.weekreport.component.entities.MsUser;
import com.neusoft.weekreport.component.entities.MsUserExample;
import com.neusoft.weekreport.component.req.RoleRefUserByRoleIdReq;
import com.neusoft.weekreport.component.req.RoleRefUserDeleteUserReq;
import com.neusoft.weekreport.component.resp.RoleRefUserByRoleIdResp;
import com.neusoft.weekreport.mapper.BgUserRefRoleMapper;
import com.neusoft.weekreport.mapper.MsUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoleRefUserBizServiceImpl implements RoleRefUserBizService {

    @Resource
    BgUserRefRoleMapper bgUserRefRoleMapper;

    @Resource
    MsUserMapper msUserMapper;

    //根据roleId查询用户
    @Override
    public RoleRefUserByRoleIdResp getUserInfoListByRoleId(RoleRefUserByRoleIdReq req) throws Exception {
        RoleRefUserByRoleIdResp resp = new RoleRefUserByRoleIdResp();
        BgUserRefRoleExample bgUserRefRoleExample = new BgUserRefRoleExample();
        BgUserRefRoleExample.Criteria criteria = bgUserRefRoleExample.createCriteria();
        criteria.andRoleIdEqualTo(req.getRoleId());

        List<BgUserRefRole> bgUserRefRoleList = bgUserRefRoleMapper.selectByExample(bgUserRefRoleExample);
        List<MsUser> msUsers =new ArrayList<>();
        if(!CollectionUtils.isEmpty(bgUserRefRoleList)){
            List<String> list = new ArrayList<>();
            for (BgUserRefRole bgUserRefRole : bgUserRefRoleList) {
                list.add(bgUserRefRole.getUserId());
            }
            MsUserExample msUserExample = new MsUserExample();
            MsUserExample.Criteria criteria1 = msUserExample.createCriteria();
            criteria1.andIdIn(list);
            if(!StringUtils.isEmpty(req.getAccountName())){
                criteria1.andAccountNameLike("%" + req.getAccountName() +"%");
            }
            if(req.getUserName()!=null){
                criteria1.andUserNameLike("%" +req.getUserName() + "%");
            }
            msUsers = msUserMapper.selectByExample(msUserExample);

        }
        resp.setMsUsers(msUsers);
        return resp;
    }

    //根据roleId和userId删除关联表
    @Transactional
    @Override
    public void deleteUserInfoByRoleId(RoleRefUserDeleteUserReq req) throws Exception {
        BgUserRefRoleExample bgUserRefRoleExample =new BgUserRefRoleExample();
        BgUserRefRoleExample.Criteria criteria = bgUserRefRoleExample.createCriteria();
        criteria.andRoleIdEqualTo(req.getRoleId());
        List<String> listUserid = req.getUserIdList();
        criteria.andUserIdIn(listUserid);
        bgUserRefRoleMapper.deleteByExample(bgUserRefRoleExample);
    }

    //查询未关联的用户
    @Override
    public RoleRefUserByRoleIdResp getUserNoByRoleId(RoleRefUserByRoleIdReq req) throws Exception {
        RoleRefUserByRoleIdResp resp= new RoleRefUserByRoleIdResp();
        BgUserRefRoleExample bgUserRefRoleExample =new BgUserRefRoleExample();
        BgUserRefRoleExample.Criteria criteria = bgUserRefRoleExample.createCriteria();
        criteria.andRoleIdEqualTo(req.getRoleId());
        List<BgUserRefRole> bgUserRefRoles = bgUserRefRoleMapper.selectByExample(bgUserRefRoleExample);

        MsUserExample msUserExample = new MsUserExample();
        MsUserExample.Criteria  criteria1 = msUserExample.createCriteria();
        criteria1.andEnableEqualTo(1);
        if(!CollectionUtils.isEmpty(bgUserRefRoles)){
            List<String> list = new ArrayList<>();
            for (BgUserRefRole bgUserRefRole : bgUserRefRoles) {
                list.add(bgUserRefRole.getUserId());
            }
            criteria1.andIdNotIn(list);
        }
        if(!StringUtils.isEmpty(req.getAccountName())){
            criteria1.andAccountNameLike("%" + req.getAccountName() +"%");
        }
        if(!StringUtils.isEmpty(req.getUserName())){
            criteria1.andUserNameLike("%" + req.getUserName() +"%");
        }
        if(req.getEnable()!=null){
            criteria1.andEnableEqualTo(req.getEnable());
        }
        List<MsUser>  msUsers = msUserMapper.selectByExample(msUserExample);
        resp.setMsUsers(msUsers);
        return resp;
    }

    //给角色添加关联用户
    @Transactional
    @Override
    public void AddUserByRoleId(RoleRefUserDeleteUserReq req) throws Exception {
        List<String> list = req.getUserIdList();
        BgUserRefRole bgUserRefRole = new BgUserRefRole();
        for (String userId : list) {
            bgUserRefRole.setRoleId(req.getRoleId());
            bgUserRefRole.setUserId(userId);
            bgUserRefRoleMapper.insert(bgUserRefRole);
        }
    }
}

